<?php
/**
 * Created by PhpStorm.
 * User: 海峡
 * Date: 2020/11/4
 * Time: 17:13
 */
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Admin\Menu;
use Illuminate\Http\Request;
use App\Models\Admin\Role;
use App\Tools\Services\JsonService;

class LoginController extends BaseController
{
    /*
     *
     */
    public function index()
    {
        return view('admin.login');
    }

    public function checkLogin(Request $request)
    {
//        if (!$request->isMethod('post')) return
        $post = $this->validate($request,[
             'username'=>'required',
             'password'=>'required',
        ],[
            'username.required'=>'账号不能为空'
        ]);
        $bool = auth()->guard('admin')->attempt($post);
        if($bool){
            if(config('rbac.super')!=$post['username']){
                $menu_ids = Role::where('id',auth()->guard('admin')->user()->role_id)->value('menus');
            }else{
                $menu_ids = Menu::pluck('id')->toArray();
            }
            session(['admin.auth'=>true,'admin.menu'=>$menu_ids]);
            return ['status'=>200,'msg'=>'登录成功'];
        }
        return ['status'=>-1,'msg'=>'登录失败'];
    }

    public function logOut(Request $request)
    {
        Cache::flush();
        return redirect('admin');
    }

}
